// pages/pages-list/enter/enter.js
import api from '../../../utils/api/api'
import tool from "../../../utils/publics/tool"
import config from "../../../config"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    entranceTime: [],
    entranceNum: [],
    bike: [], // 上传的车身
    info: {
      id: '1', // 活动id  
      real_name: '', // 姓名 
      mobile: '', // 手机号 
      card_no: '', // 身份证  
      license: '', // 驾照  
      slide: '', // 生活照
      declaration: '', // 宣言
      enroll_date: '', // 参与日期  
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options,"options");
    let info = this.data.info;
    info.id = options.id || 1;
    info.mobile = getApp().store.getState().userInfo.mobile || '';
    this.setData({
      info,
      id: options.id 
    })
    this.getData();
    return
    // let array = JSON.parse(options.jkl_detail);
    // let  entranceTime = Object.keys(array);
    // let  entranceTime2 =  Object.keys(array);
    // let  entranceNum = Object.values(array);
    // entranceTime.forEach((item,index)=>{
    //   console.log( item+'     '+entranceNum[index]);
    //   entranceTime[index] =  item+'   门票剩余  '+ entranceNum[index] + '张';
    // })
    // let info = this.data.info;
    // info.id = options.id
    // info.mobile = getApp().store.getState().userInfo.mobile || '';
    // this.setData({
    //   info,
    //   entranceTime,
    //   entranceTime2,
    //   entranceNum,
    // })
  },
  // 获取数据
  getData() {
    tool.loading()
    api.jkl_detailt({
      id: this.data.id
    }).then(res => {
      let {
        code,
        data,
        msg
      } = res;
      if (code == 1) {
        let array =data.stock;
        let entranceTime = Object.keys(array);
        let entranceTime2 = Object.keys(array);
        let entranceNum = Object.values(array);
        let info = this.data.info;
        entranceTime.forEach((item, index) => {
          entranceTime[index] = item + '   门票剩余  ' + entranceNum[index] + '张';
        });
        if(data.join_detail.aid){
          info.enroll_date = data.join_detail.enroll_date;
          info.card_no = data.join_detail.card_no;
          info.mobile = data.join_detail.mobile;
          info.real_name = data.join_detail.real_name;
          info.license =  data.join_detail.license;
          info.declaration =  data.join_detail.declaration;
        }
        this.setData({
          entranceTime,
          entranceTime2,
          info,
          bike:data.join_detail.slide || []
        })
        tool.loading_h()
      }
     
    })
  },
  // 输入框监听
  bindinput(e) {
    let key = e.currentTarget.dataset.name;
    let value = e.detail.value;
    let info = this.data.info;
    info[key] = value;
    this.setData({
      info
    })

  },
  // 上传驾驶证
  uploadDrive() {
    tool.chooseImage(1).then(res => {
      tool.loading()
      tool.uploadFiles(res.tempFilePaths, `${config.REQUESTURL}api/upload`).then(res => {
        console.log(res, "REs");
        let info = this.data.info;
        info.license = res[0];
        tool.loading_h()
        this.setData({
          info
        })
      })
    })
  },
  // 删除驾驶证
  deImg(e) {
    console.log(e);
    let type = e.currentTarget.dataset.type;
    let index = e.currentTarget.dataset.index;
    if (type == 2) {
      let bike = this.data.bike;
      bike.splice(index, 1);
      this.setData({
        bike
      })
    } else {
      let info = this.data.info;
      info.license = '';
      this.setData({
        info: {}
      })
    }

  },
  // 上传车手照
  uploadLicense() {
    let bike = this.data.bike;
    tool.chooseImage(3 - bike.length).then(res => {
      tool.loading()
      tool.uploadFiles(res.tempFilePaths, `${config.REQUESTURL}api/upload`).then(res => {
        bike = [...bike, ...res];
        tool.loading_h()
        this.setData({
          bike
        })
      })
    })
  },
  // 选择日期
  bindPickerChange: function (e) {
    let info = this.data.info;
    info.enroll_date = this.data.entranceTime2[e.detail.value];
    this.setData({
      index: e.detail.value,
      info
    })
  },
  // 购买
  buy() {
    let info = this.data.info;
    let regIdNo = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; // 身份证正则验证
    info.slide = this.data.bike.join(",");
    for (var k in info) {
      if (!info[k]) return tool.alert("请填写信息");
    };
    if (!regIdNo.test(info.card_no)) {
      return tool.alert("请输入正确的身份证号");
    }
    api.jkl_submit(info).then(res => {
      let {
        code,
        data,
        msg
      } = res;
      if (code == 1) {
        wx.requestPayment({
          ...data,
          success(res) {
            tool.alert("报名成功", 1);
            tool.jump_red(`/pages/pages-list/success2/success2?id=${info.id}`)
          },
          fail(res) {
            tool.jump_rel(`/pages/pages-list/huizhan-details2/huizhan-details2?id=${info.id}}`, )
          }
        })
      }
    })

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})